const App = getApp();
const WXAPI = require("../../api/home");
const baseUrl = require("../../api/config").dev; //这里用的是ES6的写法
var amapFile = require("../../libs/amap-wx.130 2.js"); //如：..­/..­/libs/amap-wx.js
import config from "../../config";

Page({
  data: {
    searchValue:'',
    showDialog: false,
    activeTabName: "0",
    serviceList: null,
    latitude: "",
    longitude: "",
    imgBaseUrl: `${baseUrl["imgBaseUrl"]}`,
    optionsList: [],
    orgList: null,
    currentCity: "",
    homeBg: `${baseUrl["baseUrl"]}/files/appletstaticimg/home-bg.png`,
    navBarHeight: "87",
    isSticky: true,
    serviceTabList: [],
  },
  // 页面加载时调用
  onLoad: function (options) {},
  // 页面初次渲染完成后调用
  onRead: function () {},
  // 页面显示时调用
  onShow: async function () {
    // 获取定位和当前区
    if (App.checkIsLogin()) {
      this.getPosition();
      this.setOptions();
      this.setServiceList();
      this.setData({
        showDialog: true,
      });
    } else {
      wx.navigateTo({
        url: "/pages/login/login",
      });
    }
    this.getDom();
  },
  // 页面隐藏时调用
  onHide: function () {},
  // 页面卸载时调用
  onUnload: function () {},
  // onPageScroll(e){
  //   console.log(e.scrollTop);
  // },
  onClose() {
    this.setData({
      showDialog: false,
    });
  },
  toOrgDetail(e){
    const {id, title, orgid } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/packageA/pages/org-detail/index?orgId=${orgid}&id=${id}&title=${title}`,
    });
  },
  jump(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    });
  },
  setOptions() {
    let list = JSON.parse(JSON.stringify(App.globalData.dictTree));
    let res = list.filter((item) => {
      return item.sKey == "fwlb";
    });
    // 手动添加推荐选项卡
    res[0].Child?.unshift({
      sName: "推荐",
      sKey: "0",
    });
    this.setData({
      optionsList: App.globalData.bannerOptions,
      serviceTabList: res[0].Child,
    });
  },
  async setServiceList(index) {
    let params = {
      PageIndex: 1,
      PageSize: 20,
      iEnabled: 1,
      iAduitState: 3,
      sCategoryCode: index && index != 0 ? index : "",
    };
    let isCustomLoading = true;
    let res = await WXAPI.getServiceList(params, isCustomLoading);
    if (res.data.code == 0) {
      let tmpList = res.data.data?.list;
      if (tmpList.length > 0) {
        for (let i = 0; i < tmpList.length; i++) {
          tmpList[i].sFileUrl = this.data.imgBaseUrl + tmpList[i].sFileUrl;
        }
      }
      this.setData({
        serviceList: tmpList,
      });
    }
  },
  changService(e) {
    this.setServiceList(e.detail.name);
  },
  updateHomeSearchValue(val){

  },
  search(e) {
    // 搜索查询
    const {path,code,keyWords} = e.detail
    wx.navigateTo({
      url: `${path}?code=${code}&keyWords=${keyWords}&isFromHomeSearch=1`,
    });
  },
  toOrg() {
    wx.navigateTo({
      url: `/packageA/pages/org/index`
    });
  },
  toService() {
    wx.navigateTo({
      url: `/packageA/pages/service-list/index`,
    });
  },
  getPosition() {
    var myAmapFun = new amapFile.AMapWX({
      key: config.gdMapKey,
    });
    let _this = this;
    myAmapFun.getRegeo({
      success: function (data) {
        //成功回调
        _this.setData({
          currentCity: data[0].regeocodeData.addressComponent.district,
          latitude: data[0].latitude,
          longitude: data[0].longitude,
        });
        _this.getList();
      },
      fail: function (info) {
        //失败回调
        console.log(info);
      },
    });
  },
  async getList() {
    let params = {
      PageIndex: 1,
      PageSize: 4,
      dLng: this.data.longitude,
      dLat: this.data.latitude,
    };
    let res = await WXAPI.getOrgList(params);
    if (res.data.code == 0) {
      this.setData({
        orgList: res.data.data.list,
      });
    }
  },
  getDom() {
    const that = this;
    const query = wx.createSelectorQuery();
    query.selectAll(".customNavRef").boundingClientRect();
    query.exec(function (res) {
      that.setData({
        navBarHeight: res[0][0].height,
      });
    });
  },
  toPosition() {
    // wx.navigateTo({
    //   url: "/packageA/pages/map-position/index"
    // });
  },
});
